﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

namespace Thor.ProviderPackager
{
    class Program
    {
        private static StreamWriter sw = null;

        static void Main(string[] args)
        {
            try
            {
                if (args.Length == 3)
                {
                    LogMessage("Getting arguments...");

                    string directoryToPackage = args[0];
                    LogMessage(string.Format("Directory to Package: {0}", directoryToPackage));

                    string packageName = args[1];
                    LogMessage(string.Format("Package Name: {0}", packageName));

                    string packageDirectory = args[2];
                    LogMessage(string.Format("Package Directory: {0}", packageDirectory));

                    if (!string.IsNullOrEmpty(directoryToPackage))
                    {
                        if (Directory.Exists(directoryToPackage))
                        {
                            if (!string.IsNullOrEmpty(packageName))
                            {
                                if (!string.IsNullOrEmpty(packageDirectory))
                                {
                                    if (!Directory.Exists(packageDirectory))
                                        Directory.CreateDirectory(packageDirectory);

                                    if (File.Exists(packageDirectory + @"\" + packageName))
                                        File.Delete(packageDirectory + @"\" + packageName);

                                    FastZip zip = new FastZip();
                                    zip.CreateZip(packageDirectory + @"\" + packageName, directoryToPackage, true, "");
                                }
                                else
                                    LogMessage("Package directory to specified.");
                            }
                            else
                                LogMessage("Package name not specified.");
                        }
                        else
                            LogMessage(string.Format("Directory to package ({0}) does not exist.", directoryToPackage));
                    }
                    else
                        LogMessage("Directory to package not specified.");
                }
                else
                {
                    LogMessage(string.Format("Invalid number of parameters ({0}).", args.Length.ToString()));

                    if (args.Length > 0)
                    {
                        for (int i = 0; i < args.Length; i++)
                        {
                            LogMessage(string.Format("Parameter {0}: {1}", (i + 1).ToString(), args[i]));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex.Message);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }

        private static void LogMessage(string message)
        {
            Console.WriteLine(message);

            if (sw == null)
            {
                string fileName = "Thor.ProviderPackager.log";

                if (File.Exists(fileName))
                    File.Delete(fileName);

                sw = new StreamWriter(fileName);
            }

            sw.WriteLine(message);
        }
    }
}
